package com.music.player;


import com.music.R;
import com.music.list.AllMusicActivity;
import com.music.main.MainActivity;
import com.music.mp3.Mp3;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
//import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Mp3PlayerActivity extends Activity{
	//布局对象
	LinearLayout l = null;
	//上一曲
	Button syq=null;
	//播放和暂停按钮
	public static Button play=null;
	//停止
	Button tz=null;
	//下一曲
	Button xyq=null;
	//播放器对象
	MediaPlayer player=null;
	//MP3文件路径
	public static String path=null;
	//显示歌名
	public static TextView name=null;
	//显示时间
	public static TextView time=null;
	//显示歌词
	public static TextView lr=null;
	//当前要播放的MP3
	public static Mp3 m=null;
	//滚动条
	public static SeekBar bar=null;
	//跳转到service的Intent
	Intent player_intent=new Intent();
	//要播放歌曲的下标，默认为0
	public static int index=0;
	//指定MP3播放的时间（毫秒）
	public static int i=0;
	//托动滚动条时间差
	public static long movetime=0;
	//托动滚动条起始时间
	public static long startmove=0;
	/** Called when the activity is first created. */
	@Override
	//初始化控件并绑定监听器
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.player);
		player_intent.setClass(this, PlayerService.class);
		name=(TextView)findViewById(R.id.name);
		time=(TextView)findViewById(R.id.time);
		syq=(Button)findViewById(R.id.syq);
		play=(Button)findViewById(R.id.play);
		tz=(Button)findViewById(R.id.tz);
		xyq=(Button)findViewById(R.id.xyq);
		bar=(SeekBar)findViewById(R.id.seek);
		lr=(TextView)findViewById(R.id.lrc);
		//拖动滚动条时触发
		bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				if(!PlayerService.stop){
					i=bar.getProgress();
					movetime+=(i-startmove);
					player_intent.putExtra("doPlayer",PlayerService.MOVE);
					startService(player_intent);
				}else{
					bar.setProgress(0);
				}	
			}
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				startmove=bar.getProgress();
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				
			}
		});
		//为按钮绑定事件
		play.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(PlayerService.isplaying){
					play.setBackgroundResource(R.drawable.play);
					//暂停操作
					player_intent.putExtra("doPlayer",PlayerService.PAUSE);
					startService(player_intent);

				}else{
					play.setBackgroundResource(R.drawable.zt);
					//播放操作
					player_intent.putExtra("doPlayer",PlayerService.PLAY);
					startService(player_intent);
					
				}
			}
		});
		syq.setOnTouchListener(new TouchButton(R.drawable.syq2, R.drawable.syq));
		syq.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(index==0){
					index=AllMusicActivity.mp3list.size()-1;
					path=getMp3Path();
				
				}else{
					index=index-1;
					path=getMp3Path();
				}
				if(PlayerService.stop || !PlayerService.isplaying){
					play.setBackgroundResource(R.drawable.zt);
				}
				player_intent.putExtra("doPlayer",PlayerService.LAST);
				startService(player_intent);
				name.setText("歌曲："+m.getMp3_name());
			}
		});
		xyq.setOnTouchListener(new TouchButton(R.drawable.xyq2, R.drawable.xyq));
		xyq.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(index==AllMusicActivity.mp3list.size()-1){
					index=0;
					path=getMp3Path();
				}else{
					index=index+1;
					path=getMp3Path();
				}
				if(PlayerService.stop || !PlayerService.isplaying){
					play.setBackgroundResource(R.drawable.zt);
				}
				player_intent.putExtra("doPlayer",PlayerService.NEXT);
				startService(player_intent);
				name.setText("歌曲："+m.getMp3_name());
			}
		});
		tz.setOnTouchListener(new TouchButton(R.drawable.tz2, R.drawable.tz));
		tz.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//停止操作
				player_intent.putExtra("doPlayer",PlayerService.STOP);
				startService(player_intent);
				play.setBackgroundResource(R.drawable.play);
			}
		});
	}
	class TouchButton implements OnTouchListener{
		int down;
		int up;
		public TouchButton(int down,int up){
			this.down=down;
			this.up=up;
		}
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if(event.getAction() == MotionEvent.ACTION_DOWN){  
                //更改为按下时的背景图片  
                v.setBackgroundResource(down);  
			  }else if(event.getAction() == MotionEvent.ACTION_UP){  
                //改为抬起时的图片  
                v.setBackgroundResource(up);  
        }  
			return false;
		}
		
	}
	/**
	 * 获得MP3路径
	 * @param m Mp3对象，用户获取要播放的MP3文件名
	 * @return MP3路径
	 */
	private String getMp3Path(){
		//获得要播放的Mp3歌曲对象
		m=AllMusicActivity.mp3list.get(index);
		//获得路径
		String path=MainActivity.SDPath+"mp3/"+m.getMp3_name()+".mp3";
		return path;
	}
	@Override
	protected void onResume() {
		if(PlayerService.player!=null){
			player_intent.putExtra("doPlayer",PlayerService.STOP);
			startService(player_intent);
		}
		Intent i=getIntent();
		index=i.getIntExtra("index",0);
		//获取列表页面传递的数据，即当前要播放的歌曲
		path=getMp3Path();
		player_intent.putExtra("doPlayer",PlayerService.CREATE);
		startService(player_intent);
		super.onResume();
	}

	 
}
